<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属物业" prop="propertyId">
        <el-select v-model="queryParams.propertyId" placeholder="请选择物业" size="mini"  @change="selectEstate">
          <el-option v-for="property in propertyListOptions"
                     :key="property.id"
                     :label="property.propertyName"
                     :value="property.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="所属小区" prop="estateId">
        <el-select v-model="queryParams.estateId" placeholder="请选择小区" style="width:140px;" size="mini" filterable clearable>
          <el-option v-for="estate in estatesOptions"
                     :key="estate.id"
                     :label="estate.estateName"
                     :value="estate.id"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['module:areaList:add']">新增</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="lineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所在物业" align="center" prop="propertyName" />
      <el-table-column label="所在小区" align="center" prop="estateName" />
      <el-table-column label="区块名称" align="center" prop="areaName" />
      <el-table-column label="区块类型" align="center" prop="buildType">
        <template slot-scope="scope">
          <div>
            <el-tag v-if="scope.row.buildType==0" style="cursor: pointer" :type="'success'">楼宇</el-tag>
            <el-tag v-if="scope.row.buildType==1" style="cursor: pointer" :type=" '' ">区域</el-tag>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <div>
            <el-tag v-if="scope.row.state === 0" style="cursor: pointer" :type="'info'">未启用</el-tag>
            <el-tag v-else style="cursor: pointer" :type=" '' ">已启用</el-tag>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['module:areaList:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['module:areaList:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList" />


    <!-- 添加或修改巡更线路对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body @close="cancel">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-col :span="24">
          <el-form-item label="所属物业" prop="propertyId">
            <el-select v-model="form.propertyId" placeholder="请选择物业" size="mini"  @change="selectEstate">
              <el-option v-for="property in propertyListOptions"
                         :key="property.id"
                         :label="property.propertyName"
                         :value="property.id"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="所属小区" prop="estateId">
            <el-select v-model="form.estateId" placeholder="请选择小区" style="width:140px;" size="mini" filterable clearable>
              <el-option v-for="estate in estatesOptions"
                         :key="estate.id"
                         :label="estate.estateName"
                         :value="estate.id"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="线路名称" prop="lineName">
            <el-input v-model="form.areaName" placeholder="请输入线路名称" size="mini"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-row v-for="(item, index) in form.listInfos" :key="item.id" style="border: solid 1px;border-radius:5px;margin-top: 10px">
            <el-col :span="20">
              <el-form-item label="区域" prop="listInfos.'+index+'.areaId" style="width:100%;margin-top: 10px;">
                <el-select v-model="item.areaId" placeholder="请选择区域" style="width:95%;" size="mini" filterable clearable >
                  <el-option v-for="area in pointList"
                             :key="area.id"
                             :label="area.areaTitle"
                             :value="area.id"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <div style="margin-top: 15px;">
                <el-button @click.prevent="removeSetting(item)" size="mini">删除</el-button>
              </div>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="24">
          <el-form-item label=" " style="margin-top: 10px">
            <el-button icon="el-icon-circle-plus-outline" @click="addSetting()" size="mini">新增区域</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="完成模式" prop="buildType" >
            <el-radio-group v-model="form.buildType" size="mini">
              <el-radio :value="0" :label="0">楼宇</el-radio>
              <el-radio :value="1" :label="1">区域</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="状态" prop="state" >
            <el-radio-group v-model="form.state" size="mini">
              <el-radio :value="1" :label="1">正常</el-radio>
              <el-radio :value="0" :label="0">停用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listPropertyNoPage} from "@/api/module/property/property";
import {listAllEstate} from "@/api/module/estate/estate";
import {addList, delList, getList, listList, updateList} from "@/api/module/event/area/areaList";
import {listArea} from "@/api/module/event/area/cleanArea";

export default {
  name: "Line",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      propertyListOptions:[],
      estatesOptions:[],
      pointList:[],
      queryParamsEstate: {
        propertyId: undefined,
      },
      queryParamArea: {
        areaType: 3,
      },
      // 总条数
      total: 0,
      // 巡更线路表格数据
      lineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaType: 0,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getPropertyList();
    this.getList();
  },
  methods: {
    /** 查询巡更线路列表 */
    getList() {
      this.loading = true;
      listList(this.queryParams).then(response => {
        this.lineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getPointList(){
      listArea(this.queryParamArea).then(response => {
        this.pointList = response.rows;
      });
    },
    getPropertyList() {
      listPropertyNoPage().then(response => {
        this.propertyListOptions = response.data;
      });
    },
    selectEstate(val){
      this.queryParamsEstate.propertyId = val;
      this.getEstateList();
    },
    getEstateList() {
      listAllEstate(this.queryParamsEstate).then(response => {
        console.log('getEstateList',response.data);
        this.estatesOptions = response.data;
      });
    },
    removeSetting(item) {
      var index = this.form.listInfos.indexOf(item)
      if (index !== -1) {
        this.form.listInfos.splice(index, 1)
      }
    },
    addSetting() {
      this.form.listInfos.push({
        'areaId': '',
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        propertyId: undefined,
        estateId: undefined,
        areaName: undefined,
        buildType: undefined,
        state: undefined,
        createTime: undefined,
        areaType:0,
        listInfos:[],
      }
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length!==1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getPointList();
      this.getEstateList();
      this.getPropertyList()
      this.open = true;
      this.title = "添加巡更线路";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      this.getEstateList();
      this.getPropertyList()
      this.getPointList();
      const id = row.id || this.ids;
      getList(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改巡更线路";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          this.form.areaType = 0;
          if (this.form.id != null) {
            updateList(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.reset();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addList(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.reset();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除巡更线路编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return delList(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
